package ma.cdm.securite;
import java.io.UnsupportedEncodingException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.HashMap;
import java.util.Map;

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.KeyGenerator;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;

import org.bouncycastle.util.encoders.Base64;


public class CDMKeyGen {
    private SecretKey key;
    private Cipher ecipher;
    private Cipher dcipher;
    
    private static Map<String,CDMKeyGen> sessionsInstances = new HashMap<String, CDMKeyGen>();
    private CDMKeyGen() throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException{
        key = KeyGenerator.getInstance("DES").generateKey();
        ecipher = Cipher.getInstance("DES");
        dcipher = Cipher.getInstance("DES");
        ecipher.init(Cipher.ENCRYPT_MODE, key);
        dcipher.init(Cipher.DECRYPT_MODE, key);
    }

    public static CDMKeyGen getInstance(String sessionId) throws NoSuchAlgorithmException, InvalidKeyException, NoSuchPaddingException {
        CDMKeyGen keyGen;
    	if(sessionsInstances.containsKey(sessionId))
    	{
    		keyGen = sessionsInstances.get(sessionId);
    	}
    	else
    	{
    		 keyGen = new CDMKeyGen();
    		 sessionsInstances.put(sessionId, keyGen);
    	}
        return keyGen;
    }

    public String encrypt(String str) throws UnsupportedEncodingException, IllegalBlockSizeException, BadPaddingException {
        byte[] utf8 = str.getBytes("UTF8");
        byte[] enc = ecipher.doFinal(utf8);
        return new String(Base64.encode(enc));
    }

    public String decrypt(String str) throws IllegalBlockSizeException, BadPaddingException, UnsupportedEncodingException {
        byte[] dec = Base64.decode(str);
        byte[] utf8 = dcipher.doFinal(dec);
        return new String(utf8, "UTF8");
    }

    

//	public static List<SecurityTokensVO> generateMatrix(String sessionId)
//    {
//    	List<SecurityTokensVO> matrix = null;
//    	try{   
//    	    CDMKeyGen keyGen = CDMKeyGen.getInstance(sessionId);  
//    	    matrix = new ArrayList<SecurityTokensVO>();
//	    	for (int i = 0; i < 10; i++) {
//	    		SecurityTokensVO svo = new SecurityTokensVO();
//	    		svo.setKey(String.valueOf(i));
//	    		svo.setValue( keyGen.encrypt(String.valueOf(i)));
//	    		matrix.add(svo);
//			}
//    	
//    	}catch(Exception e)
//    	{
//    		e.printStackTrace();
//    		matrix = new ArrayList<SecurityTokensVO>();
//    		for (int i = 0; i < 10; i++) {
//    			SecurityTokensVO svo = new SecurityTokensVO();
//	    		svo.setKey(String.valueOf(i));
//	    		svo.setValue(String.valueOf(i));
//	    		matrix.add(svo);
//    		}
//    		
//    	}
//    	finally{
//    		return matrix;
//    	}
//    	
//    }
    
    public static void main(String[] args) throws InvalidKeyException, NoSuchAlgorithmException, NoSuchPaddingException, UnsupportedEncodingException, IllegalBlockSizeException, BadPaddingException {
        CDMKeyGen keyGen = CDMKeyGen.getInstance("1");
        String string = "1";
        String enc = keyGen.encrypt(string);
        System.out.println(enc);
        String dec = keyGen.decrypt(enc);
        System.out.println(dec);
        
        CDMKeyGen keyGen1 = CDMKeyGen.getInstance("1");
        String string1 = "1";
        String enc1 = keyGen1.encrypt(string1);
        System.out.println(enc1);
        String dec1 = keyGen1.decrypt(enc1);
        System.out.println(dec1);
    }
}